﻿using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Goldenlight.Diablo.Data;

namespace Microsoft.Goldenlight.Diablo.AnimationViewer
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public Body CurrentBody
        {
            get
            {
                return bodyComboBox.SelectedItem as Body;
            }
        }

        public Animation CurrentAnimation
        {
            get
            {
                return animationsListBox.SelectedItem as Animation;
            }
        }

        public AnimationFrame CurrentAnimationFrame
        {
            get
            {
                return animationFramesListBox.SelectedItem as AnimationFrame;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void HandleWindowLoaded(object sender, RoutedEventArgs e)
        {
            using (var dataContext = new AnimationDataContext())
            {
                bodyComboBox.DisplayMemberPath = "EnglishName";
                foreach (var body in dataContext.Bodies.ToArray())
                {
                    bodyComboBox.Items.Add(body);
                }
            }
        }

        private void HandleBodyComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            animationsListBox.Items.Clear();
            if (e.AddedItems.Count > 0)
            {
                var body = e.AddedItems[0] as Body;
                if (body != null)
                {
                    using (var dataContext = new AnimationDataContext())
                    {
                        animationsListBox.DisplayMemberPath = "Action.EnglishName";
                        foreach (var animation in dataContext.Animations.Include("Action").Where(anim => anim.BodyId == body.Id).ToArray())
                        {
                            animationsListBox.Items.Add(animation);
                        }
                    }
                }
            }
        }

        private void HandleAnimationsListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            animationFramesListBox.Items.Clear();
            if (e.AddedItems.Count > 0)
            {
                var animation = e.AddedItems[0] as Animation;
                if (animation != null)
                {
                    using (var dataContext = new AnimationDataContext())
                    {
                        animationFramesListBox.DisplayMemberPath = "Id";
                        foreach (var animationFrame in dataContext.AnimationFrames.Include("AnimationImage").Where(animFrame => animFrame.AnimationId == animation.Id).ToArray())
                        {
                            animationFramesListBox.Items.Add(animationFrame);
                        }
                    }
                }
            }
        }

        private void HandleAnimationFramesListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                var animationFrame = e.AddedItems[0] as AnimationFrame;
                if (animationFrame != null && animationFrame.AnimationImage != null)
                {
                    animationImage.Source = BitmapDecoder.Create(new MemoryStream(animationFrame.AnimationImage.Bitmap), BitmapCreateOptions.None, BitmapCacheOption.Default).Frames[0];
                    animationImage.RenderTransformOrigin = new Point(animationFrame.CenterX, animationFrame.CenterY);
                    if (CurrentAnimation != null)
                    {
                        animationCanvas.Width = CurrentAnimation.MainWidth;
                        animationCanvas.Height = CurrentAnimation.MainHeight;
                        Canvas.SetLeft(animationImage, CurrentAnimation.MainCenterX - animationFrame.CenterX);
                        Canvas.SetTop(animationImage, CurrentAnimation.MainCenterY - animationFrame.CenterY);
                    }
                    else
                    {
                        animationCanvas.Width = 480;
                        animationCanvas.Height = 320;
                        Canvas.SetLeft(animationImage, 240 - animationFrame.CenterX);
                        Canvas.SetTop(animationImage, 160 - animationFrame.CenterY);
                    }
                }
            }
        }
    }
}